<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 17/11/16
 * Time: 15:08
 */

Yaf_Loader::import(CONTROLLERS_PATH.'Controller.php');

class PriorController extends Controller
{
    public function init()
    {
        $this->authentication = $this->authentication();
        if(!$this->authentication)
        {
            $this->redirect('/');
            exit;
        }
        $this->prior = new PriorModel();
        $this->backdrop = new BackdropModel();
        $this->getView()->assign("user", $this->authentication['data']);
        $this->group = new GroupModel();
        $group = $this->group->listsAction($this->authentication['data']);
        $this->getView()->assign("group", $group);
    }

    public function myAction()
    {
        $page=$this->getRequest()->get("page");
        $myAction = $this->prior->myAction($this->authentication['data'],$page);
        $this->getView()->assign("myAction", $myAction);
        $this->getView()->assign("title", '培训');
    }

    public function issueAction()
    {
        $page=$this->getRequest()->get("page");
        $issue = $this->prior->issueAction($this->authentication['data'],$page);
        $this->getView()->assign("issue", $issue);
        $this->getView()->assign("title", '往期');
    }

    public function waitAction()
    {
        $page=$this->getRequest()->get("page");
        $wait = $this->prior->waitAction($this->authentication['data'],$page);
        $this->getView()->assign("wait", $wait);
        $this->getView()->assign("title", '待培训');
    }

    public function endAction()
    {
        $page=$this->getRequest()->get("page");
        $end = $this->prior->endAction($this->authentication['data'],$page);
        $this->getView()->assign("end", $end);
        $this->getView()->assign("title", '已培训');
    }

    public function addAction()
    {
        $users = $this->prior->usersAction($this->authentication['data']);
        $backdrop = $this->backdrop->listsAction($this->authentication['data']);
        $this->getView()->assign("users", $users);
        $this->getView()->assign("backdrop", $backdrop['lists']);
        $this->getView()->assign("imgPath", HTTP_WEB.VERSION_LIVESTAR.'/Default/');
        $this->getView()->assign("title", '添加');
    }

    public function addPostAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'gid' => 'require|number|egt:1|exist:group,id',
                'bid' => 'require|number|egt:1|exist:backdrop,id',
                'name' => 'require|chsSymbol|min:2|max:16',
                'date' => 'require|date|after:-1day',
                'start_time' => 'require|dateFormat:H:i:s',
                'end_time' => 'require|dateFormat:H:i:s',
                'password' => 'number|length:6|between:100000,999999',
                'section' => 'chsAlpha|max:16',
                'number' => 'require|number|between:1,2048',
                'volume' => 'require|number|between:0,1',
                'introduction' => 'max:255',
            ]);
            $validate->message([
                'gid.require' => '分组ID不能为空',
                'gid.number' => '分组ID只能是数字',
                'gid.egt' => '分组ID错误',
                'gid.exist' => '分组ID不存在',
                'bid.require' => '图片ID不能为空',
                'bid.number' => '图片ID只能是数字',
                'bid.egt' => '图片ID错误',
                'bid.exist' => '图片ID不存在',
                'name.require' => '课程名称不能为空',
                'name.chsSymbol' => '课程名称只允许汉字、字母、数字和全角标点符号',
                'name.min' => '课程名称不能小于2个字符',
                'name.max' => '课程名称不能超过16个字符',
                'date.require' => '课程日期不能为空',
                'date.date' => '课程日期格式不正确',
                'date.after' => '课程日期不能晚于今日',
                'start_time.require' => '开始时间不能为空',
                'start_time.dateFormat' => '开始时间格式不正确',
                'end_time.require' => '结束时间不能为空',
                'end_time.dateFormat' => '结束时间格式不正确',
                'password.number' => '密码只能是数字',
                'password.length' => '密码长度只能为6位',
                'password.between' => '密码不在范围内',
                'section.chsAlpha' => '自填部门只允许汉字、字母',
                'section.max' => '自填部门不能超过16个字符',
                'number.require' => '培训人数不能为空',
                'number.number' => '培训人数类型错误',
                'number.between' => '培训人数不在范围内',
                'volume.require' => '考卷类型不能为空',
                'volume.number' => '考卷类型错误',
                'volume.between' => '考卷类型错误不在范围内',
                'introduction.max' => '培训内容不能超过255个字符',
            ]);
            if ($validate->check($content)) {
                $data = $this->prior->addPostAction($content,$this->authentication['data']);
                if(!empty($data['content'])){
                    $push['llid'] = $data['llid'];
                    $push['content'] = $data['content'];
                    $send = $this->prior->sendSmsAction($push);
                    $this->bindipSend($send['push']);
                    unset($data['content']);
                }
                $code = 200;
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    public function editAction()
    {
        $llid=$this->getRequest()->get("llid");
        $prior = $this->prior->editAction($llid,$this->authentication['data']);
        $users = $this->prior->usersAction($this->authentication['data']);
        $backdrop = $this->backdrop->listsAction($this->authentication['data']);
        $this->getView()->assign("users", $users);
        $this->getView()->assign("backdrop", $backdrop['lists']);
        $this->getView()->assign("imgPath", HTTP_WEB.VERSION_LIVESTAR.'/Default/');
        $this->getView()->assign("prior", $prior);
        $this->getView()->assign("title", '修改');
    }

    public function editPostAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'llid' => 'require|number|egt:1|exist:live_list,id',
                'gid' => 'require|number|egt:1|exist:group,id',
                'bid' => 'require|number|egt:1|exist:backdrop,id',
                'name' => 'require|chsSymbol|min:2|max:16',
                'date' => 'require|date|after:-1day',
                'start_time' => 'require|dateFormat:H:i:s',
                'end_time' => 'require|dateFormat:H:i:s',
                'section' => 'chsAlpha|max:16',
                'number' => 'require|number|between:1,2048',
                'volume' => 'require|number|between:0,1',
                'introduction' => 'max:255',
            ]);
            $validate->message([
                'llid.require' => '课程不能为空',
                'llid.number' => '课程类型错误',
                'llid.egt' => '课程错误',
                'llid.exist' => '课程不存在',
                'gid.require' => '分组ID不能为空',
                'gid.number' => '分组ID只能是数字',
                'gid.egt' => '分组ID错误',
                'gid.exist' => '分组ID不存在',
                'bid.require' => '图片ID不能为空',
                'bid.number' => '图片ID只能是数字',
                'bid.egt' => '图片ID错误',
                'bid.exist' => '图片ID不存在',
                'name.require' => '课程名称不能为空',
                'name.chsSymbol' => '课程名称只允许汉字、字母、数字和全角标点符号',
                'name.min' => '课程名称不能小于2个字符',
                'name.max' => '课程名称不能超过16个字符',
                'date.require' => '课程日期不能为空',
                'date.date' => '课程日期格式不正确',
                'date.after' => '课程日期不能晚于今日',
                'start_time.require' => '开始时间不能为空',
                'start_time.dateFormat' => '开始时间格式不正确',
                'end_time.require' => '结束时间不能为空',
                'end_time.dateFormat' => '结束时间格式不正确',
                'section.chsAlpha' => '自填部门只允许汉字、字母',
                'section.max' => '自填部门不能超过16个字符',
                'number.require' => '培训人数不能为空',
                'number.number' => '培训人数类型错误',
                'number.between' => '培训人数不在范围内',
                'volume.require' => '考卷类型不能为空',
                'volume.number' => '考卷类型错误',
                'volume.between' => '考卷类型错误不在范围内',
                'introduction.max' => '培训内容不能超过255个字符',
            ]);
            if ($validate->check($content)) {
                $data = $this->prior->editPostAction($content,$this->authentication['data']);
                if(!empty($data['content'])){
                    $push['llid'] = $content['llid'];
                    $push['content'] = $data['content'];
                    $send = $this->prior->sendSmsAction($push);
                    $this->bindipSend($send['push']);
                    unset($data['content']);
                }
                $code = 200;
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    public function delAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'llid' => 'require|number|egt:1|exist:live_list,id',
            ]);
            $validate->message([
                'llid.require' => '课程不能为空',
                'llid.number' => '课程类型错误',
                'llid.egt' => '课程错误',
                'llid.exist' => '课程不存在',
            ]);
            if ($validate->check($content)) {
                if($this->authentication['data']['type']){
                    $llid = $content['llid'];
                    $data = $this->prior->delAction($this->authentication['data'],$llid);
                    $code = 200;
                }else{
                    $code = 400;
                    $data['message'] = '权限不足,只有管理员可操作';
                }
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    public function searchAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'keywords' => 'chsAlphaNum|max:16',
            ]);
            $validate->message([
                'keywords.require' => '搜索内容不能为空',
                'keywords.chsAlphaNum' => '搜索内容只能是汉字、字母和数字',
                'keywords.max' => '搜索内容不可超过16个字符',
            ]);
            if ($validate->check($content)) {
                $data = $this->prior->usersAction($this->authentication['data'],$content);
                $code = 200;
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }
}